﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class DeviceAddViewModel : NotificationObject
    {
        private DeviceAdd gWindow;
        private DeviceViewModel gDeviceViewModel;

        public DeviceAddViewModel(DeviceAdd pWindow, DeviceViewModel pDeviceViewModel)
        {
            gWindow = pWindow;
            gDeviceViewModel = pDeviceViewModel;

            Initialize();
        }

        public void Initialize()
        {
            HttpResponse response = API.SelectDeviceType();
            if(response.S == HttpResponse.Success)
            {
                DeviceType = JSON.parse<ObservableCollection<CDeviceType>>(response.D);
            }

            response = API.SelectProvince();
            if (response.S == HttpResponse.Success)
            {
                Province = JSON.parse<ObservableCollection<CProvince>>(response.D);
            }
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private int deviceTypeUID;
        public int DeviceTypeUID
        {
            get
            {
                return deviceTypeUID;
            }

            set
            {
                deviceTypeUID = value;
                RaisePropertyChanged("DeviceTypeUID");
            }
        }

        private ObservableCollection<CDeviceType> deviceType = new ObservableCollection<CDeviceType>();
        public ObservableCollection<CDeviceType> DeviceType
        {
            get
            {
                return deviceType;
            }

            set
            {
                deviceType = value;
                RaisePropertyChanged("DeviceType");
            }
        }

        private int provinceUID;
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }

            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        private ObservableCollection<CProvince> province = new ObservableCollection<CProvince>();
        public ObservableCollection<CProvince> Province
        {
            get
            {
                return province;
            }

            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        private int cityUID;
        public int CityUID
        {
            get
            {
                return cityUID;
            }

            set
            {
                cityUID = value;
                RaisePropertyChanged("CityUID");
            }
        }

        private ObservableCollection<CCity> city = new ObservableCollection<CCity>();
        public ObservableCollection<CCity> City
        {
            get
            {
                return city;
            }

            set
            {
                city = value;
                RaisePropertyChanged("City");
            }
        }

        private int officeUID;
        public int OfficeUID
        {
            get
            {
                return officeUID;
            }

            set
            {
                officeUID = value;
                RaisePropertyChanged("OfficeUID");
            }
        }

        private ObservableCollection<COffice> office = new ObservableCollection<COffice>();
        public ObservableCollection<COffice> Office
        {
            get
            {
                return office;
            }

            set
            {
                office = value;
                RaisePropertyChanged("Office");
            }
        }

        private String address;
        public String Address
        {
            get
            {
                return address;
            }

            set
            {
                address = value;
                RaisePropertyChanged("Address");
            }
        }

        private List<bool> time = new List<bool>() {false, false, false, false, false, false,
                                                    false, false, false, false, false, false,
                                                    false, false, false, false, false, false,
                                                    false, false, false, false, false, false};
        public List<bool> Time
        {
            get
            {
                return time;
            }

            set
            {
                time = value;
                RaisePropertyChanged("Time");
            }
        }

        protected DelegateCommand<object> _provinceChanged;
        public DelegateCommand<object> _ProvinceChanged
        {
            get
            {
                return _provinceChanged ?? (_provinceChanged = new DelegateCommand<object>(ProvinceChanged));
            }
        }

        protected virtual void ProvinceChanged(object obj)
        {
            City.Clear();
            Office.Clear();
            HttpResponse response = API.SelectCity(ProvinceUID);
            if (response.S == HttpResponse.Success)
            {
                if (response.D != "")
                {
                    List<CCity> list = JSON.parse<List<CCity>>(response.D);
                    foreach (var item in list)
                    {
                        City.Add(item);
                    }
                }
            }
        }

        protected DelegateCommand<object> _cityChanged;
        public DelegateCommand<object> _CityChanged
        {
            get
            {
                return _cityChanged ?? (_cityChanged = new DelegateCommand<object>(CityChanged));
            }
        }

        protected virtual void CityChanged(object obj)
        {
            Office.Clear();
            HttpResponse response = API.SelectOffice(ProvinceUID, CityUID);
            if (response.S == HttpResponse.Success)
            {
                if (response.D != "")
                {
                    List<COffice> list = JSON.parse<List<COffice>>(response.D);
                    foreach (var item in list)
                    {
                        Office.Add(item);
                    }
                }
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(ExcuteSaveCommand));
            }
        }

        protected virtual void ExcuteSaveCommand(object obj)
        {
            HttpResponse response = API.InsertDevice(new CDevice(Name, DeviceTypeUID, OfficeUID, Address));

            if(response.S == HttpResponse.Success)
            {
                List<CDeviceTime> list = new List<CDeviceTime>();
                for (int i = 0; i < 24; i++)
                {
                    if(Time[i])
                    {
                        list.Add(new CDeviceTime(Convert.ToInt16(response.D), i.ToString()));
                    }
                }
                API.InsertDeviceTime(list);
            }

            gDeviceViewModel.UpdateList();
            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExcuteQuitCommand));
            }
        }

        protected virtual void ExcuteQuitCommand(object obj)
        {
            gWindow.Close();
        }
    }
}
